﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac.Builder;
using MyAlbum.Services;
using MyAlbum.Model;
using PersonalAlbum;
using MyAlbum.Logging;
using PersonalAlbum.Caching;
using System.Web.Security;
using PersonalAlbum.Controllers;
using MyAlbum.Data;
using Autofac;
using Subtext.TestLibrary;

namespace UnitTests
{
    public class TestConfig : IDisposable
    {
        public static string AppDataPhysicalPath = @"e:\Documents and Settings\Golda\My Documents\Projects\PersonalAlbumSolution\UnitTests\TempTestData";
        public static string ConnectionString = @"Data Source=tal-pc\sqlexpress;Initial Catalog=PersonalAlbum;Integrated Security=True";
        internal static MyAlbum.Services.PersistenceModes _mode = PersistenceModes.LinqToSql;
        private HttpSimulator simulator;
        public TestConfig()
        {
            simulator = new HttpSimulator("", @"e:\Documents and Settings\Golda\My Documents\Projects\PersonalAlbumSolution\UnitTests\");
        }
        public void Initialize(MyAlbum.Services.PersistenceModes mode)
        {
            _mode = mode;
        }

        ~TestConfig()
        {
            if (simulator != null)
            {
                simulator.Dispose();
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (simulator != null)
            {
                simulator.Dispose();
                simulator = null;
            }
        }

        #endregion
    }
    public class ObjectFactory
    {
        //private static IKernel kernel;
        private static IContainer _container;
        static ObjectFactory()
        {
            if (_container == null)
            {
                var builder = new ContainerBuilder();
                //this will register all the controllers
                if (TestConfig._mode == PersistenceModes.LinqToSql)
                {
                    RegisterLinqRepositories(builder);
                }
                else if (TestConfig._mode == PersistenceModes.Xml)
                {
                    RegisterXmlRepositories(builder);
                }
                RegisterServices(builder);
                RegisterTools(builder);
                _container = builder.Build();
            }
            //    if (kernel == null)
            //    {
            //        kernel = new StandardKernel(new CustomModule());
            //    }
        }

        private static void RegisterServices(ContainerBuilder builder)
        {
            //  builder.Register<ConfigService>().As<IDataService<Config>>();
            builder.Register<EntranceService>().As<IDataService<Entrance>>();
            builder.Register<TagEntryService>().As<ITagEntryService>();
           
            builder.Register<EntryService>().As<IEntryService>();
            builder.Register<SectionService>().As<IDataService<Section>>();
            builder.Register<AlbumService>().As<IAlbumService>();
            builder.Register<SupportService>().As<IDataService<Support>>();
            builder.Register<ReplyService>().As<IDataService<Reply>>();
        }

        private static void RegisterTools(ContainerBuilder builder)
        {
            builder.Register<DemoCacheService>().As<ICacheService>();
            builder.Register<DemoLanguageService>().As<ILanguageService>();
            builder.Register<DemoMembership>().As<MembershipProvider>();
            builder.Register<FormsAuthenticationWrapper>().As<IFormsAuthentication>();
            builder.Register<DemoLogger>().As<ILogger>();
            builder.Register<TestUser>().As<IUser>().ContainerScoped();
        }

        private static void RegisterXmlRepositories(ContainerBuilder builder)
        {
            builder.Register(c => TestConfig.AppDataPhysicalPath).As<string>();
            builder.Register<MyAlbum.Data.Xml.TagEntryRepository>().As<ITagEntryRepository>();
            builder.Register<MyAlbum.Data.Xml.SectionRepository>().As<ISectionRepository>();
            builder.Register<MyAlbum.Data.Xml.AlbumRepository>().As<IAlbumRepository>();
            builder.Register<MyAlbum.Data.Xml.SupportRepository>().As<IRepository<Support>>();
            builder.Register<MyAlbum.Data.Xml.ReplyRepository>().As<IRepository<Reply>>();
            builder.Register<MyAlbum.Data.Xml.EntranceRepository>().As<IRepository<Entrance>>();
            builder.Register<MyAlbum.Data.Xml.EntryRepository>().As<IEntryRepository>();

        }

        private static void RegisterLinqRepositories(ContainerBuilder builder)
        {
            builder.Register(c => new MyAlbum.Data.Linq.EntryRepository(TestConfig.ConnectionString)).As<IEntryRepository>();
            builder.Register(c => new MyAlbum.Data.Linq.TagEntryRepository(TestConfig.ConnectionString)).As<ITagEntryRepository>();
            builder.Register(c => new MyAlbum.Data.Linq.SectionRepository(TestConfig.ConnectionString)).As<ISectionRepository>();
            builder.Register(c => new MyAlbum.Data.Linq.AlbumRepository(TestConfig.ConnectionString)).As<IAlbumRepository>();
            builder.Register(c => new MyAlbum.Data.Linq.ConfigRepository(TestConfig.ConnectionString)).As<IRepository<Config>>();
            builder.Register(c => new MyAlbum.Data.Linq.SupportRepository(TestConfig.ConnectionString)).As<IRepository<Support>>();
            builder.Register(c => new MyAlbum.Data.Linq.ReplyRepository(TestConfig.ConnectionString)).As<IRepository<Reply>>();
            builder.Register(c => new MyAlbum.Data.Linq.EntranceRepository(TestConfig.ConnectionString)).As<IRepository<Entrance>>();
        }

        private static void RegisterConrollers(ContainerBuilder builder)
        {
            builder.Register<HomeController>();           
            builder.Register<EntryController>();
            builder.Register<AlbumsController>();
            builder.Register<CategoriesController>();
            builder.Register<AccountController>();
            builder.Register<ReplyController>();
            builder.Register<RssController>();
            builder.Register<SupportController>();
        }
        public static T GetInstance<T>()
        {
            //    return kernel.Get<T>();
            //  return (T)(object)new Lang();
            return _container.Resolve<T>();
        }
#if Autofac
        public static object GetInstance(Type type)
        {
            
            return _container.Resolve(type);

        //    return kernel.Get(type);
        }
#endif
    }
}
